package com.design.softwaredesign.utils.security;

import lombok.Data;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/3 16:17
 */
@Data
public class AES {
    private String sKey = "1472583695639852";
    private String userid;
    private Integer logintime;
    private Integer tokentime;

    // 加密
    public String Encrypt(String userid, String sKey) throws Exception {
        if (userid == null) {
            System.out.print("userid为空null");
            return null;
        }

        String srcPre = UUID.randomUUID().toString().substring(0, 8);

        this.logintime = (int) (System.currentTimeMillis() / 1000);
        String code = srcPre + ";" + userid + ";" + logintime;

        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(code.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String token, String sKey) throws Exception {
        try {
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(token);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Integer authernticate(String token, String sKey) {
        String originalString = "";
        try {
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(token);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                originalString = new String(original,"utf-8");
            } catch (Exception e) {
                return -3;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return -3;
        }
        String strs[] = originalString.split(";");
        this.userid = strs[1];
        String timeStr = strs[2];
        Integer nowTime = (int) (System.currentTimeMillis() / 1000);
        this.tokentime = Integer.parseInt(timeStr);
        if(nowTime - tokentime > 172800) {
            return -2;
        }
        return 1;
    }
//    public static void main(String[] args) throws Exception {
//        AES aes = new AES();
//        String userid = "6551asda454";
//        String token = aes.Encrypt(userid, aes.getSKey());
//        System.out.println(aes.authernticate("sEfGJ4DDf3rM9XnkAG4b3h+MsFlbZcMpIUtuAl8xNds=", aes.getSKey()));
//        System.out.println(aes.userid);
////        /*
////         * 此处使用AES-128-ECB加密模式，key需要为16位。
////        */
////        String cKey = "1234567890123456";
////        // 需要加密的字串
////        String cSrc = "18251104126";
////        System.out.println(cSrc);
////        // 加密
////        String enString = AES.Encrypt(cSrc, cKey);
////        System.out.println("加密后的字串是：" + enString);
////
////        // 解密
////        String DeString = AES.Decrypt(enString, cKey);
////        System.out.println("解密后的字串是：" + DeString);
//    }
}
